/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.panel.grid;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Observable;
import org.rails.extjs.Option;
import org.rails.extjs.panel.grid.column.Column;

public class ColumnModel extends Observable {
	
	protected List<Column> columns = new ArrayList<Column>();

	public ColumnModel() {
		super();
	}
	
	public void setDefaultSortable(Boolean defaultSortable){
		addOption("defaultSortable",defaultSortable);
	}
	
	public Boolean isDefaultSortable(){
		return getBoolean("defaultSortable",false);
	}
	
	public void setDefaultWidth(Integer defaultWidth){
		addOption("defaultWidth",defaultWidth);
	}
	
	public Integer getDefaultWidth(){
		return getInteger("defaultWidth",100);
	}
	
	public void setDefaults(Option defaults){
		addOption("defaults",defaults);
	}
	
	public Option getDefaults(){
		return (Option)getOption("defaults");
	}
	
	public void addColumn(Column column){
		columns.add(column);
	}
	
	public List<Column> getColumn(){
		return columns;
	}
	
	public Column getColumn(int index){
		return columns.get(index);
	}
	
	public void removeColumn(int index){
		columns.remove(index);
	}
	
	public void insertColumn(int index,Column column){
		columns.add(index,column);
	}
	
	public void clearColumns(){
		columns.clear();
	}

	@Override
	public String getScript() {
		createItems();
		script = new StringBuffer("");
		addOption("columns",columns);
		script.append("new Ext.grid.ColumnModel(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
